package com.ticket.api.service.Impl;

import com.ticket.api.service.TblMsgContentService;
import com.ticket.common.string.StringUtil;
import com.ticket.common.time.CommonFunction;
import com.ticket.db.dao.TbUserInfoMapper;
import com.ticket.db.dao.TblMsgContentMapper;
import com.ticket.db.entity.TblMsgContent;
import com.ticket.db.entity.TblUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

@Service
public class TblMsgContentServiceImpl implements TblMsgContentService {
    private static Logger logger = LoggerFactory.getLogger(TblMsgContentServiceImpl.class);
    @Autowired
    TblMsgContentMapper contentMapper;
    @Autowired
    TbUserInfoMapper userInfo;

    @Override
    public TblMsgContent findTblMsgContentByCondition(TblMsgContent content) {
        System.err.println(content);
        return contentMapper.findTblMsgContentByCondition(content);
    }

    @Override
    public Integer insertTblMsgContent(TblMsgContent content) {
        try {
            String userId1=content.getUserId1();
            String userId2=content.getUserId2();
            if (userId1 == null || userId2 == null) {return null; }
            String front = StringUtil.Arrays(userId1,userId2);//第一个用户
            String behind = front == userId1?userId2:userId1;//第一个用户
           int isCount = findUserId1AndUserid2(front,behind);
            if(isCount==-1){
                return null;
            }else if(isCount==1){
                return 1;
            }
            TblMsgContent msg= new TblMsgContent(front,behind);
            msg= findTblMsgContentByCondition(msg);
            if(msg!=null){return -1;}
            content.setUserId1(front); content.setUserId2(behind);
            String time= CommonFunction.getLocalTime14();
            content.setCreateTime(time);
            content.setCreateUser(userId1);
            content.setLastUpdateUser(userId1);
            content.setLastUpdateTime(time);
            Integer count= contentMapper.insertTblMsgContent(content);
            return count;
        }catch (Exception e){
            logger.error("TblMsgContentServiceImpl>insertTblMsgContent:"+e);
            return -2;
        }

    }

    @Override
    public Integer updateTblMsgContent(TblMsgContent content) {
        String time= CommonFunction.getLocalTime14();
        content.setCreateTime(time);
        content.setCreateUser(content.getUserId1());
        content.setCreateTime(time);
        content.setLastUpdateUser(content.getUserId1());
        return contentMapper.updateTblMsgContent(content);

    }
    @Override
    public List<TblMsgContent> findByUserId(String  userId){
        return contentMapper.findByUserId(userId);
    }

    @Override
    public int findUserId1AndUserid2(String userId1, String userId2) {
            if(userId1==userId2 ){
                return -1;
            }
        return contentMapper.findUserId1AndUserid2(userId1,userId2)==null?0:1;
    }
}
